﻿
function $n(nid){
    return document.getElementsByTagName(nid);
}
function getPageSize(){
	var $bo=document.body;
	var $de=document.documentElement;
	maxWidth=Math.max($bo.scrollWidth,$bo.offsetWidth,$de.clientWidth,$bo.clientWidth);
	maxHeight=Math.max($bo.scrollHeight,$bo.offsetHeight,$de.clientHeight,$bo.clientHeight);
	var arrayPageSize = new Array(maxWidth,maxHeight,$de.scrollTop,$de.clientHeight);
	return arrayPageSize;
}
function showsimplebox(objLink){
	selects = $n("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility="hidden";
	}
	var ps = getPageSize();
	$("overlay").style.width= (ps[0] + 'px');
	$("overlay").style.height=(ps[1] + 'px');
	$("overlay").style.display="block";
	imgPreload = new Image();
	imgPreload.onload=function(){
	    var imgbox=$("simplebox")
		imgbox.src=objLink.href||objLink.getAttribute("href");
		
		imgbox.style.top=ps[2]+((ps[3]-imgPreload.height)*0.382)+"px";
	    imgbox.style.left=(ps[0]-imgPreload.width)*0.5+"px";
	    imgbox.style.display="block";
		return false;
	}
	imgPreload.src=objLink.href||objLink.getAttribute("href");
	
}
function hidesimplebox(){
    var imgbox=$("simplebox");
	imgbox.style.display="none";
	imgbox.removeAttribute("src");
	$("overlay").style.display="none";
	selects = $as.call(document,"select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility="visible";
	}
}
function initsimplebox(){
	var anchors = $as.call(document,"img");
	
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "simplebox")){
		    
			anchor.onclick = function () {
			    showsimplebox(this); 
			    return false;
			}
		}
	}
	var objBody = $n("body").item(0); 
	var oOvl = document.createElement("div");
	oOvl.setAttribute('id','overlay');
	oOvl.setAttribute('style','position:absolute;top:0;left:0;z-index:500;background:#000;-moz-opacity: 0.75;opacity: 0.75;display:none');
	oOvl.style.position="absolute";
	oOvl.style.background="#000";
	oOvl.style.top=0;
	oOvl.style.left=0;
	oOvl.style.zIndex="500";
	oOvl.style.filter="Alpha(Opacity=75)";
	oOvl.style.display="none";
	
	oOvl.onclick = function () {
	    hidesimplebox(); 
	    return false;
	}
	
	objBody.insertBefore(oOvl, objBody.firstChild);
	var oSbx = document.createElement("img");
	oSbx.setAttribute('id','simplebox');
	oSbx.setAttribute('style','position: absolute;z-index:600;border:6px solid #FFF;display:none;');
	oSbx.style.position="absolute";
	oSbx.style.zIndex="600";
	oSbx.style.border="6px solid #FFF";
	oSbx.style.display="none";
	
	
	oSbx.onclick = function () {hidesimplebox(); return false;}
	objBody.insertBefore(oSbx, oOvl.nextSibling);
}
addLoadEvent(initsimplebox);